import { requestApi } from '../../../api';
import { formatImage } from '../../../utils/formatImage';
const app = getApp();

Page({
    data: {
        channel: 'wx_xcx',
        navIndex: 0,
        cateIndex: 0,
        categoryList: [],
        cateList: [],
        brandList: [],
        lists: [],
        typeInfo: {},
        typeList: [],
        gender_id: ''
    },
    onLoad (options) {  
        let channel = options.channel || 'wx_xcx';

        this.setData({
            channel
        })

        this.getBrandHall();
        this.getTypeData();
    },
    // 分类馆
    // getCategoryHall() {
    //     wx.showLoading({
    //         title: '加载中...',
    //     });

    //     app.request({
    //         url: requestApi.categoryHall,
    //         data: {
    //             is_new_ver: true
    //         }
    //     }, (d) => {
    //         if (d.original.ret_code == 200) {
    //             this.setData({
    //                 categoryList: [].concat([{cate_name: "品牌", cate_id: -1}], d.original.data)
    //             });
    //         }
    //     }, (error) => {
    //         wx.showModal({
    //             title: '提示',
    //             showCancel: false,
    //             content: error,
    //         })
    //     }, () => {
    //         wx.hideLoading();
    //     })
    // },
    // 获取类目列表
    getTypeData () {
        const { channel } = this.data;

        wx.showLoading({
            title: '加载中...',
        });

        app.request({
            url: requestApi.TypeListUrl,
            data: {
                channel: channel,
            },
            login: false,
        }, (data) => {
            if(data.original.ret_code == 200) {
                const d = data.original.data;
                const navList = [{cate_name: "品牌", cate_id: -1}];

                d.forEach(item => {
                    item.cover_image.full_path = formatImage.cdnImage(item.cover_image.path, formatImage.THUMBNAIL_L);

                    navList.push({
                        cate_name: item.type_name,
                        cate_id: item.id
                    })
                    item.child_type_info.forEach(v => {
                        v.child_type_info.forEach(idm => {
                            idm.cover_image = formatImage.cdnImage(idm.cover_image, formatImage.THUMBNAIL_L);
                        })
                    })
                })

                this.setData({
                    categoryList: navList,
                    typeList: d
                })
            }


        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading();
        })
    },
    // 品牌馆
    getBrandHall() {
        const { cateIndex } = this.data;
        wx.showLoading({
            title: '加载中...',
        });

        app.request({
            url: requestApi.brandHall,
            data: {
                id: (cateIndex + 1),
                is_new_ver: true
            }
        }, (d) => {
            if (d.original.ret_code == 200) {
                d.original.data.item_list.forEach((item, index) => {
                    if (item.type == 331) {
                        item.data.cover_image.full_path = formatImage.cdnImage(item.data.cover_image.path, formatImage.THUMBNAIL_M);
                    }
                    item.name = '' + item.type + index + (item.data.name_e ? item.data.name_e : '');
                })

                this.setData({
                    cateList: [].concat(d.original.data.cate_list, [{title: '全部品牌', index: -1}]),
                    brandList: d.original.data.item_list
                });
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading();
        })
    },
    /**
     * 查询商品
     */
    searchGoods: function (e) {
        const { channel } = this.data;
        const keywords = e.detail.value;
        let arr = [], history = wx.getStorageSync('searchHistory')

        arr = [].concat([keywords], history);
        wx.setStorageSync("searchHistory", arr);
        wx.navigateTo({
            url: '/pages/categoryHall/categoryHall?keywords=' + keywords + '&channel=' + channel,
        })
    },
    // 选择分类
    chooseCategory(e) {
        const { index } = e.currentTarget.dataset;
        const { typeList } = this.data;

        if (index > 0) {
            const info = typeList.filter(item => item.id == index);

            if (info) {
                this.setData({
                    typeInfo: info[0],
                    gender_id: info[0].search_condition.gender_id ? info[0].search_condition.gender_id : ""
                })
            }
        }

        this.setData({
            scrollTop: 0,
            navIndex: index,
        })
    },
    typeSearchGoods(e) {
        const { channel, gender_id } = this.data;
        const { search , keywords } = e.currentTarget.dataset;
        const type = { 10001: '箱包', 10002: '鞋履', 10003: '配饰', 10004: '服装', 10005: '珠宝首饰', 10006: '美容', 10007: '腕表', 10008: '生活用品' };
        let url = `/pages/categoryHall/categoryHall?channel=${channel}`;

        if (!keywords) {
            url += `&keywords=${type[search.goods_type_id.split(',')[0]]}`;
        } else {
            url += `&keywords=${keywords}`;
        }

        if (gender_id) {
            url += `&gender_id=${gender_id}`;
        }

        if (search.goods_type_id && search.brand_id) {
            url += `&goods_type_id=${search.goods_type_id}`;
        }

        wx.navigateTo({
            url
        })
    },
    // 选择品牌
    chooseBrand(e) {
        const { index } = e.currentTarget.dataset;

        if (index < 4) {
            this.setData({
                cateIndex: index,
            });

            this.getBrandHall();
        } else {
            wx.navigateTo({
                url: '../searchBrands/searchBrands',
            })
        }
    },
    jumpBrandHall(e) {
        const { channel } = this.data;
        const brand_name = e.currentTarget.dataset.brand_name;

        wx.navigateTo({
            url: '/pages/categoryHall/categoryHall?keywords=' + brand_name + '&channel=' + channel,
        })
    },
    bindImgerror(e) {
        const { index } = e.currentTarget.dataset;
        const { brandList } = this.data;
        const str = `brandList[${index}]`
        console.log(e);
    }
})